Skip to content

[PyTorch Debug] Add scale_inv_std stat and skip NVFP4 layers in LogFp8TensorStats - #3044

Closed
pggPL wants to merge 12 commits into
NVIDIA:mainfrom
pggPL:debug_log_fp_fixes
Closed

pggPL wants to merge 12 commits into
NVIDIA:mainfrom
pggPL:debug_log_fp_fixes

Conversation

@pggPL

@pggPL pggPL commented May 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Addresses items 1 and 2 of #2801:

  • Adds scale_inv_std for delayed/current FP8 scaling, MXFP8, FP8 block scaling and NVFP4, including supported _columnwise variants. NVFP4 also gains scale_inv_min and scale_inv_max.
  • Allows LogFp8TensorStats and LogNvfp4TensorStats to share a layer selection. On NVFP4 layers, the FP8 logger skips bare stats with a warning and preserves FP8-recipe-prefixed what-if stats such as mxfp8_mse.

Scale standard deviation uses population variance, so a single scale has std=0. Microbatch/rank reduction uses the Chan/Welford combination. This also fixes the existing between-group sign error and preserves the sample-variance convention of the high-precision variance/std statistics.

Per-block metadata dumping (item 3 of #2801) remains outside this PR.

Validation

  • Blackwell (gb102): tests/pytorch/debug/test_log.py — 31 passed, 8 skipped. Tested the changed Python features with TE 2.20.0.dev0 / PyTorch 2.14.0a0 native binaries from the current TE devel image.
  • Full PYTHON_ONLY=1 qa/L0_pytorch_lint/test.sh: passed. Black 24.4.2: passed.
  • Tests cover unequal microbatches, every supported recipe, rowwise/columnwise scales, zero-padded scale storage, and simultaneous FP8/NVFP4 logging.
  • The numerical test reference excludes zero scale entries consistently with the logger.
  • The high-precision reduction regression checks reduced statistics against concatenated tensors.

Type of change

  • Bug fix
  • New feature
  • Documentation updates

pggPL and others added 2 commits May 26, 2026 15:08
…8TensorStats (NVIDIA#2801)

- Register scale_inv_std (plus helper variance/numel/sum buffers using
  Welford reduction) for all FP8 recipes and NVFP4 in
  add_scale_inv_stats. Population variance keeps std=0 for delayed/current
  scaling where scale_inv is a single scalar.
- Also wire scale_inv_min/max/std for NVFP4 (was previously only FP8
  recipes).
- LogFp8TensorStats.inspect_tensor now filters bare stats on NVFP4 layers
  with a warning instead of raising, so dual LogFp8TensorStats +
  LogNvfp4TensorStats configs work with overlapping (or catch-all) layer
  regexes. Recipe-prefixed FP8 stats (e.g. mxfp8_mse) are preserved for
  what-if comparisons.
- Numerics test extended to validate scale_inv_min/max/std against
  torch.std(scale_inv, unbiased=False).

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented May 26, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@greptile-apps

greptile-apps Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the previously reported defensive-check and zero-padding issues are fully addressed.

Summary

  • Registers scale inverse min, max, standard deviation, variance, count, and sum helpers for supported recipes.
  • Excludes zero-padded scale-storage entries from statistical reductions.
  • Uses the correct Chan/Welford parallel variance formula and variance convention.
  • Adds numeric coverage across recipes, directions, padding, and unequal microbatches.
  • Both previous findings are resolved: the defensive ValueError was restored, and padded zeros are excluded from the scale statistics.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    Q[Quantized tensor] --> S[Select rowwise or columnwise scale_inv]
    S --> M[Remove zero padding]
    M --> L[Per-microbatch min, max, variance, count, sum]
    L --> W[Chan/Welford combination]
    W --> O[Logged scale_inv_min, scale_inv_max, scale_inv_std]

    N[NVFP4 layer in LogFp8TensorStats] --> F{Stat has explicit FP8 recipe prefix?}
    F -->|Yes| K[Keep for what-if comparison]
    F -->|No| D[Warn and skip]
Loading

Reviews (7) · Last reviewed commit: "[PyTorch Debug] Fix scale-stat reference..."

Parallel-group variance is Sigma n_i*(var_i + (mean_i - mean)^2) / N -
the between-group term must be added, not subtracted. Single-group
buffers hide the bug (mean_i = mean_global so the term is 0); it
surfaces with scale_inv_std reduced across microbatches/ranks, where
negative variance flows into sqrt() and yields NaN.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>

def check_if_stat_is_supported(self, stat: str):
"""Returns True if stat is supported, raises ValueError otherwise."""
bare = stat[: -len("_columnwise")] if stat.endswith("_columnwise") else stat

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mse_columnwise and underflow%_columnwise should not pass this function but they would with the logic as written.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added them to exception, even though we should have stats for them I guess, but this is not scope of the PR.

pggPL and others added 3 commits June 3, 2026 12:03
…validation

compute_variance/compute_std now combine on M2 with an explicit `unbiased` flag.
Previously sample variances (torch.var default, used by variance/std) were fed into
a population-style combine, so reductions across microbatches/ranks were silently
wrong whenever group means differed. scale_inv_* combinators now pass unbiased=False
to match their population feeders. Adds test_stats_computation_microbatch_reduction
which sweeps the stats registry and checks every aux-free combinator against its own
compute fn over the concatenation.

NVFP4 check_if_stat_is_supported no longer accepts mse_columnwise /
underflows%_columnwise: only scale_inv_* have a columnwise variant, the others are
computed from the single quantized tensor and would crash downstream.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>

# Conflicts:
#	transformer_engine/debug/features/utils/stats_computation.py
…tyle

Collapse import/list/call reflows that were split by an accidental black run at
the default line length (88) instead of the repo's 100; no functional change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
pggPL and others added 4 commits June 3, 2026 13:56
…d reduction test

Route every scale_inv stat (min/max/std/variance/numel/sum) through a shared
real_scale_inv() helper that strips the zero padding MXFP8/NVFP4 quantizers add,
so padding no longer deflates the mean or corrupts the numel-weighted variance
reduction across microbatches/ranks. Add a parametrized test driving the
aux-dependent parallel-variance combine for scale_inv_std and checking it
against std over the concatenated scale_inv values.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…unbiased convention

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…ch reduction test

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@pggPL
pggPL requested a review from negvet June 9, 2026 09:02
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Ignore zero scale entries in numerical references consistently with the logger. Cover all recipes, both scale orientations, padding, and combined FP8/NVFP4 logging. Report unsupported NVFP4 stats with ValueError.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL pggPL closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants